package com.zbkj.crmeb.task.order;


import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.zbkj.crmeb.agent.model.AgentAccount;
import com.zbkj.crmeb.agent.model.AgentFreezeBill;
import com.zbkj.crmeb.agent.service.AgentAccountFlowService;
import com.zbkj.crmeb.agent.service.AgentAccountService;
import com.zbkj.crmeb.agent.service.AgentFreezeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.bill.model.MerFreezeBill;
import com.zbkj.crmeb.bill.service.MerFreezeBillService;
import com.zbkj.crmeb.chant.model.ChannelMerchantAccount;
import com.zbkj.crmeb.chant.service.IChannelMerchantAccountService;
import com.zbkj.crmeb.enums.AccountFlowTypeEnum;
import com.zbkj.crmeb.store.service.AccountFlowService;

import lombok.extern.slf4j.Slf4j;

/**
 * 解冻商户冻结金额
 */
@Component
@Configuration
@Slf4j
public class UnfreezeMerFreezeAccountTask {

    @Autowired
    MerFreezeBillService merFreezeBillService;

    @Autowired
    IChannelMerchantAccountService channelMerchantAccountService;

    @Autowired
    private AccountFlowService accountFlowService;

    @Autowired
    AgentAccountService agentAccountService;
    @Autowired
    AgentFreezeBillService agentFreezeBillService;
    @Autowired
    AgentAccountFlowService agentAccountFlowService;

    //5分钟同步一次数据
	@XxlJob("unfreezeMerFreezeAccountTask")
    public void init(){
        log.info("-----------------执行解冻金额定时任务-------------------");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //解冻一天前的数据
        calendar.add(Calendar.DATE, -1);
//        calendar.add(Calendar.MINUTE, 3);
        //商户待解冻账单
        List<MerFreezeBill> merFreezeBillList = merFreezeBillService.list(Wrappers.<MerFreezeBill>lambdaQuery().eq(MerFreezeBill::getFrozen, 0)
                .lt(MerFreezeBill::getStartThawTime,calendar.getTime()));
        if(CollectionUtils.isNotEmpty(merFreezeBillList)){
            merFreezeBillList.forEach(e->{
                ChannelMerchantAccount merchantAccount = new ChannelMerchantAccount();
                merchantAccount.setMerId(e.getMerId()).setFreezeAccount(e.getAmountFrozen().negate()).setWithdrawAccount(e.getAmountFrozen());
                channelMerchantAccountService.calcMoney(merchantAccount);
                //商户流水记录
                accountFlowService.saveAccountFlow(e.getMerId(),e.getOrderId(),null,null,
                        AccountFlowTypeEnum.PRODUCT,e.getAmountFrozen(),null,e.getAmountFrozen().negate(),"冻结金额解冻");
                log.info("冻结余额转入可用余额,冻结账单id:{},商户:{},金额:{}",e.getId(),e.getMerId(),e.getAmountFrozen());
                //更新冻结账单
                e.setFrozen(1);
                merFreezeBillService.updateById(e);
            });
        }
        //服务商待解冻账单
        List<AgentFreezeBill> agentFreezeBillList = agentFreezeBillService.list(Wrappers.<AgentFreezeBill>lambdaQuery().eq(AgentFreezeBill::getFrozen, 0)
                .lt(AgentFreezeBill::getStartThawTime,calendar.getTime()));
        if(CollectionUtils.isNotEmpty(agentFreezeBillList)){
            agentFreezeBillList.forEach(e->{
                AgentAccount agentAccount = new AgentAccount();
                agentAccount.setAgentId(e.getAgentId()).setFreezeAccount(e.getAmountFrozen().negate()).setWithdrawAccount(e.getAmountFrozen());
                agentAccountService.calcMoney(agentAccount);
                //商户流水记录
                agentAccountFlowService.saveAccountFlow(e.getAgentId(),e.getOrderId(),null,null,
                        AccountFlowTypeEnum.PRODUCT,e.getAmountFrozen(),e.getAmountFrozen().negate(),"冻结金额解冻");
                log.info("冻结余额转入可提现余额,冻结账单id:{},服务商:{},金额:{}",e.getId(),e.getAgentId(),e.getAmountFrozen());
                //更新冻结账单
                e.setFrozen(1);
                agentFreezeBillService.updateById(e);
            });
        }

    }

}
